import grace from "../../utils/grace.js"
grace.page({
      data: {
            category: [],
            active_index: 0
      },
      onLoad: function(options) {
            wx.showNavigationBarLoading();
            this.loadCategory();
      },
      loadCategory: function() {
            this.$http.get('/api/goods/category_list', {}).then((res) => {
                  this.$data.category = res.list;
                  this.loadData();
            });
      },
      onPullDownRefresh: function() {
            this.$data.category[this.$data.active_index]['page'] = 1;
            this.loadData();
      },
      onReachBottom: function() {
            let cur_goods = this.$data.category[this.$data.active_index];
            if (cur_goods['goods'] && cur_goods['total'] != cur_goods['goods'].length) {
                  let page = cur_goods['page'] ? cur_goods['page'] : 1;
                  this.$data.category[this.$data.active_index]['page'] = page + 1;
                  this.loadData();
            }
      },
      loadData: function() {
            let cur_cat = this.$data.category[this.$data.active_index];
            if (cur_cat) {
                  let cat_id = cur_cat['cat_id'];
                  let page = cur_cat['page'] ? cur_cat['page'] : 1;
                  this.$http.get('/api/goods/list', {
                        cat_id: cat_id,
                        pageSize: 10,
                        pageCurrent: page
                  }).then((res) => {
                        const data = page == 1 ? res.list : cur_cat['goods'].concat(res.list);
                        this.$data.category[this.$data.active_index]['goods'] = data;
                        this.$data.category[this.$data.active_index]['total'] = res.total;
                  });
            }
      },
      bindChange: function(e) {
            this.$data.active_index = e.currentTarget.dataset.index;
            let cur_goods = this.$data.category[this.$data.active_index];
            if (cur_goods['goods'].length == 0) {
                  this.loadData();
            }
      },
      // 增加数量
      addCount: function(e) {
            let cur_cat = this.$data.category[this.$data.active_index];
            if (cur_cat) {
                  const index = e.currentTarget.dataset.index;
                  let goods = cur_cat['goods'];
                  if (goods && index <= goods.length) {
                        let goods_num = goods[index]['num'] + 1;
                        this.$data.category[this.$data.active_index]['goods'][index]['num'] = goods_num;
                        this.addToCart(goods[index]['goods_id'], goods_num);
                  }
            }
      },
      // 减少数量
      minusCount: function(e) {
            let cur_cat = this.$data.category[this.$data.active_index];
            if (cur_cat) {
                  const index = e.currentTarget.dataset.index;
                  let goods = cur_cat['goods'];
                  if (goods && index <= goods.length) {
                        if (goods[index]['num'] > 1) {
                              let goods_num = goods[index]['num'] - 1;
                              this.$data.category[this.$data.active_index]['goods'][index]['num'] = goods_num;
                              this.addToCart(goods[index]['goods_id'], goods_num);
                        }
                  }
            }
      },
      addToCart: function(goods_id, goods_num) {
            if (!goods_id) return;
            this.$http.post('api/cart/add_goods', {
                  goods_vals: goods_id,
                  goods_number: goods_num,
                  is_cumulation: 0
            }).then((res) => {
                  this.toast('操作成功');
            });
      },
      openDetail: function(e) {
            let goods_id = e.currentTarget.dataset.goods;
            wx.navigateTo({
                  url: '../product_details/product_details?goods_id=' + goods_id,
            })
      },

})